package com.fire.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fire.dao.SystemDao;
import com.fire.pojo.User;
import com.fire.service.SystemService;
import com.fire.util.MsgInfo;
import com.fire.util.base.BooleanUtil;
import com.fire.util.base.MD5Util;
import com.fire.util.base.ResultBody;
import com.fire.util.base.SendSmsUtil;
import com.fire.util.base.TokenUtil;

@Service
public class SystemServiceImpl implements SystemService {

	@Autowired
	private SystemDao systemDao;

	/**
	 * 供应商登录接口
	 */
	@Override
	public ResultBody login(JSONObject params) throws Exception {
		ResultBody result = new ResultBody();
		// 手机号
		String phoneNum = params.getString("phoneNum");
		// 密码
		String password = params.getString("password");

		if (!BooleanUtil.StringIsNa(phoneNum) || !BooleanUtil.StringIsNa(password)) {
			result.setCode(MsgInfo.z_paramExcerror_code);
			result.setMessage(MsgInfo.z_paramExcerror_msg);
			return result;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phoneNum", phoneNum);
		map.put("password", MD5Util.GetMD5Code(password));

		User validUser = systemDao.checkUserIsAlive(map);

		if (null != validUser && validUser.getUseFlag() == 1) {
			// 创建token
			String token = TokenUtil.createJwt(validUser.getId(), System.currentTimeMillis() + 3600000);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("token", token);
			resultMap.put("isIdentify", validUser.getIsIdentify());

			if (validUser.getIsIdentify() == 0) {
				// 未通过认证
				resultMap.put("userInfo", validUser);
			} else if (validUser.getIsIdentify() == 1) {
				// 已认证
				Map<String, Object> userInfo = systemDao.getIdentifyUserInfo(validUser.getId());
				resultMap.put("userInfo", userInfo);
			}

			result.setData(resultMap);
		} else {
			result.setCode(MsgInfo.z_login_error_code);
			result.setMessage(MsgInfo.z_login_error_msg);
		}

		return result;
	}

	/**
	 * 供应商注册接口
	 */
	@Override
	public ResultBody register(JSONObject params) throws Exception {
		ResultBody result = new ResultBody();
		// 手机号
		String phoneNum = params.getString("phoneNum");
		// 密码
		String password = params.getString("password");

		if (!BooleanUtil.StringIsNa(phoneNum) || !BooleanUtil.StringIsNa(password)) {
			result.setCode(MsgInfo.z_paramExcerror_code);
			result.setMessage(MsgInfo.z_paramExcerror_msg);
			return result;
		}

		// 判断当前用户是否已经注册过
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phoneNum", phoneNum);
		map.put("password", MD5Util.GetMD5Code(password));

		int isExist = systemDao.checkPhoneIsExist(map);
		if (isExist > 0) {
			result.setCode(MsgInfo.z_exist_error_code);
			result.setMessage(MsgInfo.z_exist_error_msg);
			return result;
		}

		// 初始化供应商信息
		User user = new User(phoneNum, MD5Util.GetMD5Code(password));
		// 注册用户
		systemDao.registerUser(user);

		return result;
	}

	/**
	 * 重置密码接口
	 */
	@Override
	public ResultBody resetPwd(JSONObject params) throws Exception {
		ResultBody result = new ResultBody();
		// 手机号
		String phoneNum = params.getString("phoneNum");
		// 密码
		String password = params.getString("password");

		if (!BooleanUtil.StringIsNa(phoneNum) || !BooleanUtil.StringIsNa(password)) {
			result.setCode(MsgInfo.z_paramExcerror_code);
			result.setMessage(MsgInfo.z_paramExcerror_msg);
			return result;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phoneNum", phoneNum);
		map.put("password", MD5Util.GetMD5Code(password));

		// 判断手机号是否有效
		Integer isExist = systemDao.checkPhoneIsExist(map);

		if (isExist == 1) {
			// 更新密码
			systemDao.resetPwd(map);
		} else {
			result.setCode(MsgInfo.z_sign_exist_code);
			result.setMessage(MsgInfo.z_sign_exist_msg);
		}

		return result;
	}

	/**
	 * 发送验证码接口
	 */
	@Override
	public ResultBody sendSmsCode(JSONObject params) throws Exception {
		ResultBody result = new ResultBody();
		// 手机号
		String phoneNum = params.getString("phoneNum");
		if (!BooleanUtil.StringIsNa(phoneNum)) {
			result.setCode(MsgInfo.z_paramExcerror_code);
			result.setMessage(MsgInfo.z_paramExcerror_msg);
			return result;
		}
		Integer code = SendSmsUtil.sendMsg(phoneNum);
		if (null != code && code.toString().length() == 6) {
			result.setData(code);
		} else {
			result.setCode(MsgInfo.z_sms_code_error_code);
			result.setMessage(MsgInfo.z_sms_code_error_msg);
		}
		return result;
	}

}
